package com.opencloud.ywt.provider.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.ResultBody;
import com.opencloud.common.utils.DateUtils;
import com.opencloud.iot.client.model.entity.IotDaySleepResult;
import com.opencloud.ywt.client.model.entity.*;
import com.opencloud.ywt.provider.service.BhyfBdbService;
import com.opencloud.ywt.provider.service.BhyfCityService;
import com.opencloud.ywt.provider.service.BhyfSportService;
import com.opencloud.ywt.provider.service.BhyfStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * 园务通（城市）
 * @author yeyong
 */
@Slf4j
@RestController
@Api(value = "园务通", tags = "园务通")
public class BhyfSportController {
    @Autowired
    private BhyfSportService bhyfSportService;
    @Autowired
    private BhyfStudentService bhyfStudentService;
    @Autowired
    private BhyfBdbService bhyfBdbService;

    /**
     * 获取学生运动结果列表
     * @return
     */
    @ApiOperation(value = "获取学生运动结果列表", notes = "获取学生运动结果列表")
    @GetMapping("/sport/personlist")
    public ResultBody<List<BhyfStudentSportInfo>> getSportPersonList(
            @RequestParam(required = true) Integer gid,
            @RequestParam(required = false) Integer gradenum,
            @RequestParam(required = false) Integer classid,
            @RequestParam(required = false) String studentname,
            @RequestParam(required = false) Integer personid,
            @RequestParam(required = true) String startTime,
            @RequestParam(required = true) String endTime,
            @RequestParam(required = true) Integer isLast
    )
    {
        if(studentname==null)studentname="";
        if(gradenum==null)gradenum=0;
        if(classid==null)classid=0;
        //运动情况
        if(isLast==1){
            startTime="2019-09-01";
            endTime=DateTime.now().toString("yyyy-MM-dd");
        }

        List<BhyfXhbSportPersonComment> sportPersonList = bhyfBdbService.findPersonSportList(gid,0,startTime,endTime);
        log.error("****************sportPersonList："+sportPersonList.size());
        List<BhyfStudentSportInfo> returnList = new ArrayList<>();
        //获取班级
        List<BhyfStudent> students = bhyfStudentService.getBhyfStudentList(gid,gradenum,classid,studentname,personid);
        for(int i=0;i<students.size();i++)
        {
            //学生信息
            BhyfStudent student = students.get(i);
            BhyfStudentSportInfo m = new BhyfStudentSportInfo();
            m.setStudent(student);
            int curpersonid = student.getPersonid();
            List<BhyfXhbSportPersonComment> studentSport = new ArrayList<>();
            List<BhyfXhbSportPersonComment> sspp = new ArrayList<>();
            studentSport = sportPersonList.stream().filter((BhyfXhbSportPersonComment s) -> s.getPersonid().equals(curpersonid)).collect(Collectors.toList());
            log.error("personid:"+curpersonid+"^^^^^^^^^^^^^^^^^^^^^^^^^^^^studentSport："+studentSport.size());
            if(isLast==1){
                if(studentSport!=null && studentSport.size()>0)
                {
                    sspp.add(studentSport.get(0));
                }
            }else{
                sspp = studentSport;
            }
            m.setSportPersonList(sspp);
            //关注状态
            /**
            List<BhyfXhbWatch> watches = bhyfStudentService.findBhyfXhbWatchList(student.getPersonid(), -1,2);
            if (watches != null && watches.size() > 0) {
                m.setWatchstatus(watches.get(0).getWatchstatus());
                m.setWatchid(watches.get(0).getId());
            }
             **/
            returnList.add(m);
        }

        return ResultBody.ok().data(returnList);
    }

    /**
     * 获取班级运动结果列表
     * @param gid
     * @param startTime
     * @param endTime
     * @return
     */
    @ApiOperation(value = "获取班级运动结果列表", notes = "获取班级运动结果列表")
    @GetMapping("/sport/classlist")
    public ResultBody<List<BhyfXhbSportClass>> getSportClassList(
            @RequestParam(required = true) Integer gid,
            @RequestParam(required = false) Integer classid,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime
    )
    {
        if(startTime==null){
            startTime = "2020-01-01";
        }
        if(endTime==null){
            endTime=DateTime.now().toString("yyyy-MM-dd");
        }
        List<BhyfXhbSportClass> bdbSportClassList = bhyfBdbService.findClassSportList(gid,classid,startTime,endTime);
        return ResultBody.ok().data(bdbSportClassList);
        //return ResultBody.ok().data(bhyfSportService.findClassSportList(gid,classid,startTime,endTime));
    }


    /**
     * 获取无效数据学生列表
     * @param startTime
     * @param endTime
     * @return
     */
    @ApiOperation(value = "获取无效数据学生列表", notes = "获取无效数据学生列表")
    @GetMapping("/sport/skippersonlist")
    public ResultBody<List<BhyfXhbSkipPerson>> getSkipPersonList(
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime
    )
    {
        return ResultBody.ok().data(bhyfSportService.findSkipPersonList(startTime,endTime));
    }

    /**
     * 获取运动配置库
     * @return
     */
    @ApiOperation(value = "获取运动配置库", notes = "获取运动配置库")
    @GetMapping("/sport/allsport")
    public ResultBody<List<BhyfXhbAllSport>> getXhbAllSportList()
    {
        return ResultBody.ok().data(bhyfSportService.findAllSportList());
    }


    /**
     * 获取园所运动配置库
     * @return
     */
    @ApiOperation(value = "获取园所运动配置库", notes = "获取园所运动配置库")
    @GetMapping("/sport/gardensport")
    public ResultBody<List<BhyfXhbGardensport>> getXhbGardenSportList(
            @RequestParam(required = true) Integer gid)
    {
        return ResultBody.ok().data(bhyfSportService.findGardenSportList(gid));
    }


    /**
     * 新增运动库
     * @param sportname
     * @param sportlogo
     * @param sportlevel
     * @param sportgrade
     * @param sporttime
     * @return
     */
    @ApiOperation(value = "新增运动库", notes = "新增运动库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sportname", value = "运动名称", required = false, paramType = "form"),
            @ApiImplicitParam(name = "sportlogo", value = "运动LOGO", required = false, paramType = "form"),
            @ApiImplicitParam(name = "sportlevel", value = "运动强度", required = false, paramType = "form"),
            @ApiImplicitParam(name = "sportgrade", value = "适用年级", required = false, paramType = "form"),
            @ApiImplicitParam(name = "sporttime", value = "适用时段", required = false, paramType = "form")
    })
    @PostMapping("/sport/addallsport")
    public ResultBody<BhyfXhbAllSport> addBhyfXhbAllSport
            (
                    @RequestParam(name = "sportname",required = false) String sportname,
                    @RequestParam(name = "sportlogo",required = false) String sportlogo,
                    @RequestParam(name = "sportlevel",required = false) String sportlevel,
                    @RequestParam(name = "sportgrade",required = false) String sportgrade,
                    @RequestParam(name = "sporttime",required = false) String sporttime
            )
    {
        BhyfXhbAllSport model = new BhyfXhbAllSport();
        model.setCreatetime(new Date());
        model.setSportgrade(sportgrade);
        model.setSportlevel(sportlevel);
        model.setSportlogo(sportlogo);
        model.setSportname(sportname);
        model.setSporttime(sporttime);
        model = bhyfSportService.addBhyfXhbAllSport(model);
        return ResultBody.ok().data(model);
    }

    /**
     * 编辑运动库
     * @param id
     * @param sportname
     * @param sportlogo
     * @param sportlevel
     * @param sportgrade
     * @param sporttime
     * @return
     */
    @ApiOperation(value = "编辑运动库", notes = "编辑运动库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "sportname", value = "运动名称", required = false, paramType = "form"),
            @ApiImplicitParam(name = "sportlogo", value = "运动LOGO", required = false, paramType = "form"),
            @ApiImplicitParam(name = "sportlevel", value = "运动强度", required = false, paramType = "form"),
            @ApiImplicitParam(name = "sportgrade", value = "适用年级", required = false, paramType = "form"),
            @ApiImplicitParam(name = "sporttime", value = "适用时段", required = false, paramType = "form")
    })
    @PostMapping("/sport/updateallsport")
    public ResultBody<Integer> updateBhyfXhbAllSport
            (
                    @RequestParam(name = "id",required = true) Integer id,
                    @RequestParam(name = "sportname",required = false) String sportname,
                    @RequestParam(name = "sportlogo",required = false) String sportlogo,
                    @RequestParam(name = "sportlevel",required = false) String sportlevel,
                    @RequestParam(name = "sportgrade",required = false) String sportgrade,
                    @RequestParam(name = "sporttime",required = false) String sporttime
            )
    {
        BhyfXhbAllSport model = bhyfSportService.findBhyfXhbAllSportById(id);
        model.setSportgrade(sportgrade);
        model.setSportlevel(sportlevel);
        model.setSportlogo(sportlogo);
        model.setSportname(sportname);
        model.setSporttime(sporttime);
        bhyfSportService.updateBhyfXhbAllSport(model);
        return ResultBody.ok().data(1);
    }


    /**
     * 编辑园所运动库
     * @param id
     * @param sportlogo
     * @param sportlevel
     * @param sportgrade
     * @param sporttime
     * @return
     */
    @ApiOperation(value = "编辑园所运动库", notes = "编辑园所运动库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "sportlogo", value = "运动LOGO", required = false, paramType = "form"),
            @ApiImplicitParam(name = "sportlevel", value = "运动强度", required = false, paramType = "form"),
            @ApiImplicitParam(name = "sportgrade", value = "适用年级", required = false, paramType = "form"),
            @ApiImplicitParam(name = "sporttime", value = "适用时段", required = false, paramType = "form")
    })
    @PostMapping("/sport/updategardensport")
    public ResultBody<Integer> updateBhyfXhbGardenSport
    (
            @RequestParam(name = "id",required = true) Integer id,
            @RequestParam(name = "sportlogo",required = false) String sportlogo,
            @RequestParam(name = "sportlevel",required = false) String sportlevel,
            @RequestParam(name = "sportgrade",required = false) String sportgrade,
            @RequestParam(name = "sporttime",required = false) String sporttime
    )
    {
        BhyfXhbGardensport model = bhyfSportService.findBhyfXhbGardenSportById(id);
        model.setSportgrade(sportgrade);
        model.setSportlevel(sportlevel);
        model.setSportlogo(sportlogo);
        model.setSporttime(sporttime);
        bhyfSportService.updateBhyfXhbGardenSport(model);
        return ResultBody.ok().data(1);
    }

    /**
     * 配置园所运动库
     * @param gid
     * @param ids
     * @return
     */
    @ApiOperation(value = "配置园所运动库", notes = "配置园所运动库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gid", value = "gid", required = true, paramType = "form"),
            @ApiImplicitParam(name = "ids", value = "ids", required = true, paramType = "form")
    })
    @PostMapping("/sport/addgardensport")
    public ResultBody<Integer> addBhyfXhbGardenSport
            (
                    @RequestParam(name = "gid",required = true) Integer gid,
                    @RequestParam(name = "ids",required = true) String ids
            )
    {
        String[] splitId=ids.split(",");
        for(String s : splitId){
            int id = Integer.parseInt(s);
            BhyfXhbAllSport sport = bhyfSportService.findBhyfXhbAllSportById(id);
            BhyfXhbGardensport model = new BhyfXhbGardensport();
            model.setGid(gid);
            model.setCreatetime(new Date());
            model.setSportgrade(sport.getSportgrade());
            model.setSportlevel(sport.getSportlevel());
            model.setSportlogo(sport.getSportlogo());
            model.setSportname(sport.getSportname());
            model.setSporttime(sport.getSporttime());
            bhyfSportService.addBhyfXhbGardenSport(model);
        }
        return ResultBody.ok().data(1);
    }


    /**
     * 删除园所运动库
     * @param id
     * @param gid
     * @return
     */
    @ApiOperation(value = "删除园所运动库", notes = "删除园所运动库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "form"),
            @ApiImplicitParam(name = "gid", value = "gid", required = true, paramType = "form")
    })
    @PostMapping("/sport/deletegardensport")
    public ResultBody<Integer> deleteBhyfXhbGardenSport
            (
                    @RequestParam(name = "id",required = true) Integer id,
                    @RequestParam(name = "gid",required = true) Integer gid
            )
    {
        bhyfSportService.deleteBhyfXhbGardenSport(id);
        return ResultBody.ok().data(1);
    }

    /**
     * 按年级获取运动的统计结果
     * @param gid
     * @param gradenum
     * @param startdate
     * @param enddate
     * @return
     */
    @ApiOperation(value = "按年级获取运动的统计结果", notes = "按年级获取运动的统计结果")
    @GetMapping("/sport/gradesportresult")
    public ResultBody<List<BhyfXhbSportPersonCommentResult>> getPersonSportCommentResult(
            @RequestParam(required = true) Integer gid,
            @RequestParam(required = false) Integer gradenum,
            @RequestParam(required = false) String startdate,
            @RequestParam(required = false) String enddate
    )
    {
        List<BhyfXhbSportPersonCommentResult> returnList = new ArrayList<>();
        if(startdate==null){
            startdate = "2020-01-01 00:00:01";
        }
        if(enddate==null){
            enddate=DateTime.now().toString("yyyy-MM-dd");
        }
        return ResultBody.ok().data(bhyfBdbService.findGradeSportList(gid,gradenum,startdate,enddate));
        //return ResultBody.ok().data(bhyfSportService.getPersonSportCommentResult(gid,gradenum,startdate,enddate));
    }
}
